<template>
<view class="body">
			<view class="header flex">
				<image src="http://imgbdb3.bendibao.com/whbdb/202011/11/20201111092541_78494.png" class="local-icon"></image>
				<view class="city" @tap="toggleAddress">{{ local }}</view>
				<image src="http://imgbdb3.bendibao.com/whbdb/202011/11/20201111092824_71951.png" class="arrow-down"></image>
			</view>
			<view class="city-select" v-if="showAddress && province">
				<view class="address flex">
					<scroll-view :scroll-y="true" class="province flex col">
						<view v-for="(item, index) in province" :key="index" :class="'name ' + (item.chouse? 'chouse':'')" @tap="addressClick" :data-index="index" data-type="province">
							{{ item.cityname }}
						</view>
					</scroll-view>
					<scroll-view :scroll-y="true" class="city flex col">
						<view v-for="(item, index) in city" :key="index" :class="'name ' + (item.chouse? 'chouse':'')" @tap="addressClick" :data-index="index" data-type="city">
							{{ item.cityname }}
						</view>
					</scroll-view>
					<scroll-view :scroll-y="true" class="qu flex col">
						<view v-for="(item, index) in district" :key="index" :class="'name ' + (item.chouse? 'chouse':'')" @tap="addressClick" :data-index="index" data-type="district">
							{{ item.cityname }}
						</view>
					</scroll-view>
				</view>
				<view class="black" @tap="toggleAddress" catchtouchmove="ture"></view>
			</view>
			<navigator class="search flex" :url="'../select/select?keywords=' + item + '&type=1&provinceName=' + provinceName + '&cityName=' + cityName + '&districtName=' + districtName + '&lat=' + lat + '&lng=' + lng + '&cityCode=' + citycode">
				<image src="/static/images/index/sousuo.png" class="search-icon"></image>
				<view class="input text-over">请输入{{ type }}名称，例如{{ type == '网点' ? '派出所' : '入户' }}</view>
				<view class="type flex">
					<text>{{ type }}</text>
					<image src="/static/images/dwicon.png" mode class="select-icon"></image>
				</view>
			</navigator>
			<!--  最近搜索 -->
			<view class="lately-search flex" v-if="history">
				<view class="lately-title">最近搜索：</view>
				<navigator v-for="(item, index) in history" :key="index" class="lately-item" :url="'../select/select?keywords=' + item.name + '&type=' + item.type + '&provinceName=' + provinceName + '&cityName=' + cityName + '&districtName=' + districtName + '&lat=' + lat + '&lng=' + lng + '&cityCode=' + citycode">
					{{ item.name }}
				</navigator>
			</view>
			<!-- 项目列表 -->
			<view v-for="(item, index) in infoList" :key="index" class="list">
				<view class="list-title flex">
					<image :src="item.icon" class="list-item-icon"></image>
					<view class="list-item-title" :style="'color:' + item.color + ';'">{{ item.name }}</view>
				</view>
				<view class="list-border flex">
					<view v-for="(item, index2) in item.list" :key="index2" class="list-item" @tap="itemClick" :data-item="item">{{ item.name }}</view>
				</view>
			</view>
		</view>
</template>

<script>
//index.js
//获取应用实例
var app = getApp();
const util = require("../../../../utils/util.js"); // var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
// var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var QQMapWX = require("../../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js");
var qqmapsdk = new QQMapWX({
  key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
var postData = {};
let typeid = 0;
let allTypeList = []; // 网点类型列表
// 网点类型列表
let defaultCity;

export default {
  data() {
    return {
      local: '深圳市',
      citycode: 'sz',
      province: [],
      provinceIndex: 0,
      city: [],
      cityIndex: 0,
      district: [],
      districtIndex: 0,
      showAddress: false,
      type: '网点',
      history: [],
      showPop: false,
      opentype: '',
      data: {},
      infoList: [{
        name: '办事',
        list: [{
          name: '派出所',
          type: 'search'
        }, {
          name: '公安局',
          type: 'search'
        }, {
          name: '街道办',
          type: 'search'
        }, {
          name: '出入境',
          type: 'search'
        }, {
          name: '教育局',
          type: 'search'
        }, {
          name: '民政局',
          type: 'search'
        }, {
          name: '公证处',
          type: 'search'
        }, {
          name: '社保局',
          type: 'search'
        }, {
          name: '车管所',
          type: 'search'
        }, {
          name: '住房建设局',
          type: 'search'
        }, {
          name: '人力资源局',
          type: 'search'
        }, {
          name: '政府',
          type: 'search'
        }, {
          name: '行政服务大厅',
          type: 'search'
        }, {
          name: '居委会',
          type: 'search'
        }, {
          name: '社区',
          type: 'search'
        }],
        icon: 'http://imgbdb3.bendibao.com/xcxbdb/file/20213/15/20210315160318_18252.jpeg',
        color: '#FE5E13'
      }, {
        name: '交通',
        list: [{
          name: '机场',
          type: 'open'
        }, {
          name: '码头',
          type: 'open'
        }, {
          name: '汽车站',
          type: 'open'
        }, {
          name: '地铁站',
          type: 'openFeature'
        }, {
          name: '火车站',
          type: 'open'
        }],
        icon: 'http://imgbdb3.bendibao.com/xcxbdb/file/20213/15/20210315160338_92774.jpeg',
        color: '#2A83FE'
      }, {
        name: '休闲',
        list: [{
          name: '全部',
          type: 'navi',
          id: ''
        }, {
          name: '公园',
          type: 'navi',
          id: '5'
        }, {
          name: '寺庙',
          type: 'navi',
          id: '50'
        }, {
          name: '人文',
          type: 'navi',
          id: '69'
        }, {
          name: '休闲',
          type: 'navi',
          id: '72'
        }, {
          name: '展览馆',
          type: 'navi',
          id: '53'
        }],
        icon: 'http://imgbdb3.bendibao.com/xcxbdb/file/20213/15/20210315160240_67445.jpeg',
        color: '#6633FF'
      }],
      provinceName: '默认',
      cityName: '深圳市',
      districtName: '全部',
      lat: '',
      lng: '',
      getCity: ""
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    uni.showLoading({
      title: '加载中',
      mask: true
    });
    util.getUrl(app.globalData.url).then(() => {
      util.request('network.php', {
        action: 'get_citys'
      }, 'POST').then(res => {
        if (res.data.state == 1) {
          res.data.data.forEach(item => item.chouse = false);
          res.data.data.unshift({
            cityname: '默认',
            citycode: 'mr',
            id: 0,
            chouse: true
          });
          this.setData({
            province: res.data.data,
            city: [{
              cityname: '深圳市',
              id: 291,
              citycode: 'sz'
            }]
          });
          defaultCity = JSON.parse(JSON.stringify(this.city));
          uni.hideLoading();
          qqmapsdk.reverseGeocoder({
            success: res => {
              uni.showLoading({
                title: '正在定位中',
                mask: true
              });
              util.request('network.php', {
                action: 'get_citys',
                proviceid: this.province.filter(item => item.cityname == res.result.address_component.province)[0].id
              }, 'POST').then(citys => {
                let localCity = citys.data.data.filter(item => item.cityname == res.result.address_component.city)[0];
                let city = {
                  cityname: res.result.address_component.city,
                  citycode: localCity.citycode,
                  id: localCity.id,
                  chouse: false
                };
                this.city = [city];
                defaultCity = JSON.parse(JSON.stringify(this.city));
                this.local = `${res.result.address_component.city}${res.result.address_component.district}`;
                this.setData({
                  city: this.city,
                  local: this.local,
                  lat: res.result.location.lat,
                  lng: res.result.location.lng,
                  citycode: localCity.citycode,
                  provinceName: this.province[this.provinceIndex].cityname,
                  cityName: this.city[this.cityIndex].cityname,
                  districtName: res.result.address_component.district
                });
                this.getDistrict(this.districtName);
                uni.hideLoading();
              });
            },
            fail: () => {
              uni.hideLoading({
                success: () => {
                  uni.showToast({
                    title: '定位失败',
                    icon: 'none'
                  });
                }
              });
            }
          });
        }
      });
    });
  },

  onShow() {
    this.history = uni.getStorageSync('history') ? JSON.parse(uni.getStorageSync('history')) : '';
    this.setData({
      history: this.history
    });
  },

  onShareAppMessage: function () {
    return {
      title: '本地宝-网点查询',
      imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
      path: `/pages/tools/wangdian/index/index`
    };
  },
  methods: {
    // 选择城市
    addressClick(e) {
      let index = util.getValue('index', e);
      let type = util.getValue('type', e);
      this[`${type}Index`] = index;
      this[type].forEach(item => item.chouse = false);
      let data = this[type][index];
      data.chouse = true;
      this.setData({//[`${type}Index`]: this.data[`${type}Index`]
        //[type]: this.data[type]
      });
      //try fix
      this[type] = this[type];
      //try fix
      this[`${type}Index`] = this[`${type}Index`];

      switch (type) {
        case 'province':
          {
            this.setData({
              district: []
            });

            if (data.cityname == '默认') {
              this.setData({
                city: defaultCity
              });
            } else {
              this.getCity();
            }
          }
          break;

        case 'city':
          this.getDistrict();
          break;

        case 'district':
          {
            this.showAddress = false;
            let district = this.district[this.districtIndex].id ? this.district[this.districtIndex].cityname : '全部';
            this.local = `${this.city[this.cityIndex].cityname}${this.district[this.districtIndex].cityname}`;
            this.setData({
              showAddress: this.showAddress,
              local: this.local,
              provinceName: this.province[this.provinceIndex].cityname,
              cityName: this.city[this.cityIndex].cityname,
              districtName: district,
              citycode: this.city[this.cityIndex].citycode
            });
          }
          break;

        default:
          {}
      }
    },

    // 获得城市
    getCity() {
      if (!this.province[this.provinceIndex].city) util.request('network.php', {
        action: 'get_citys',
        proviceid: this.province[this.provinceIndex].id
      }, 'POST').then(res => {
        if (res.data.state == 1) {
          res.data.data.forEach(item => item.chouse = false);
          this.city = res.data.data;
          this.province[this.provinceIndex].city = res.data.data;
          this.setData({
            city: this.city,
            getCity: this.province
          });
        }
      });else {
        this.city = this.province[this.provinceIndex].city;
        this.setData({
          city: this.city
        });
      }
    },

    // 获得区
    getDistrict(district) {
      if (this.city[this.cityIndex].district) {
        this.district = this.city[this.cityIndex].district;
        this.districtIndex = this.district.findIndex(item => item.cityname == district);
        this.setData({
          district: this.city[this.cityIndex].district,
          districtIndex: this.districtIndex
        });
      } else {
        util.request('network.php', {
          action: 'get_citys',
          cityid: this.city[this.cityIndex].id
        }, 'POST').then(res => {
          if (res.data.state == 1) {
            res.data.data.forEach(item => item.chouse = false);
            res.data.data.unshift({
              id: '',
              citycode: '',
              cityname: '全市'
            });
            this.district = res.data.data;
            this.city[this.cityIndex].district = this.district;
            this.districtIndex = this.district.findIndex(item => item.cityname == district);
            this.setData({
              district: this.district,
              city: this.city,
              districtIndex: this.districtIndex
            });
          }
        });
      }
    },

    itemClick(e) {
      let item = util.getValue("item", e);
      let name = item.name;
      let type = item.type;
      let id = item.id;

      if (type == 'search') {
        uni.navigateTo({
          url: `/pages/tools/wangdian/select/select?keywords=${name}&type=1&provinceName=${this.provinceName}&cityName=${this.cityName}&districtName=${this.districtName}&lat=${this.lat}&lng=${this.lng}&cityCode=${this.citycode}`,
          success: () => util.addHistory({
            name: name,
            type: 1
          })
        });
      } else if (type == 'navi') {
        let areaid = this.district[this.districtIndex] ? this.district[this.districtIndex].id : '';
        uni.navigateTo({
          url: `/pages/tools/hdcx/hdcx?type=1&jingdian=${id}&citycode=${this.city[this.cityIndex].citycode}&cityname=${this.city[this.cityIndex].cityname}&areaid=${areaid}`
        });
      } else {
        uni.navigateTo({
          url: '../souvenir/souvenir?name=' + name
        });
      } // if (type == 'openFeature') util.openMap(type);
      // else util.openMap('open', {name});

    },

    // 开关选择城市
    toggleAddress() {
      this.setData({
        showAddress: !this.showAddress
      });
    },

    onPageScroll() {}

  }
};
</script>
<style>
.body {
  position: relative;
  font-size: 30rpx;
}
.body .header {
  padding-left: 40rpx;
  height: 90rpx;
  line-height: 90rpx;
}
.body .header .local-icon {
  width: 22rpx;
  height: 26rpx;
  margin-right: 14rpx;
}
.body .header .city {
  color: #252525;
}
.body .header .arrow-down {
  width: 21rpx;
  height: 12rpx;
  margin-left: 12rpx;
}
.body .city-select {
  position: absolute;
  width: 100%;
  background-color: #fff;
  z-index: 1;
}
.body .city-select .address {
  border-bottom: 2rpx solid #ccc;
  color: #555;
}
.body .city-select .address .chouse {
  color: #f60;
}
.body .city-select .address .province,
.body .city-select .address .city,
.body .city-select .address .qu {
  height: 50vh;
  flex: 1;
  text-align: center;
  overflow-y: scroll;
  overflow-x: hidden;
}
.body .city-select .address .province .name,
.body .city-select .address .city .name,
.body .city-select .address .qu .name {
  width: 100%;
  padding: 20rpx 0;
  border-bottom: 2rpx solid #f1f1f1;
}
.body .city-select .address .province {
  flex-basis: 28%;
  background-color: #f1f1f1;
}
.body .city-select .address .city {
  padding-left: 18rpx;
  flex-basis: 28%;
  background-color: #fafafa;
}
.body .city-select .address .qu {
  padding-left: 18rpx;
  flex-basis: 44%;
  text-align: left;
}
.body .city-select .address .qu .name {
  padding-left: 11rpx;
}
.body .black {
  height: 50vh;
  background-color: rgba(0,0,0,0.5);
}
.body .search {
  height: 80rpx;
  padding-left: 30rpx;
  margin: 0 40rpx;
  margin-bottom: 34rpx;
  line-height: 80rpx;
  background-color: #f7f7f7;
  border-radius: 8rpx;
}
.body .search .search-icon {
  width: 32rpx;
  height: 32rpx;
}
.body .search .type {
  color: #7a7a7a;
}
.body .search .type .select-icon {
  width: 16rpx;
  height: 12rpx;
  margin-left: 12rpx;
  vertical-align: middle;
}
.body .search .type text {
  flex-shrink: 0;
}
.body .search .input {
  width: 467rpx;
  height: 42rpx;
  margin-left: 18rpx;
  margin-right: 23rpx;
  color: #bdbdbd;
  line-height: 42rpx;
  border-right: 2rpx solid #d4d4d4;
}
.body .lately-search {
  height: 114rpx;
  flex-wrap: wrap;
  margin: 0 40rpx;
  margin-bottom: 70rpx;
  overflow-y: hidden;
}
.body .lately-search .lately-title {
  font-size: 26rpx;
  height: 48rpx;
  margin-bottom: 18rpx;
  color: #7a7a7a;
  line-height: 48rpx;
}
.body .lately-search .lately-item {
  font-size: 22rpx;
  min-width: 118rpx;
  height: 48rpx;
  margin-right: 18rpx;
  margin-bottom: 18rpx;
  color: #bdbdbd;
  line-height: 48rpx;
  text-align: center;
  background-color: #f7f7f7;
  border-radius: 8rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
  white-space: nowrap;
}
.body .list {
  margin: 0 40rpx;
  color: #252525;
}
.body .list .list-title {
  margin-bottom: 42rpx;
  font-weight: 550;
  font-size: 34rpx;
}
.body .list .list-title .list-item-icon {
  width: 36rpx;
  height: 38rpx;
  margin-right: 22rpx;
}
.body .list .list-border {
  flex-wrap: wrap;
}
.body .list .list-border .list-item:nth-child(2) {
  border-left: 2rpx solid #dfdfdf;
  border-right: 2rpx solid #dfdfdf;
}
.body .list .list-border .list-item:nth-child(5) {
  border-left: 2rpx solid #dfdfdf;
  border-right: 2rpx solid #dfdfdf;
}
.body .list .list-border .list-item:nth-child(8) {
  border-left: 2rpx solid #dfdfdf;
  border-right: 2rpx solid #dfdfdf;
}
.body .list .list-border .list-item:nth-child(11) {
  border-left: 2rpx solid #dfdfdf;
  border-right: 2rpx solid #dfdfdf;
}
.body .list .list-border .list-item:nth-child(14) {
  border-left: 2rpx solid #dfdfdf;
  border-right: 2rpx solid #dfdfdf;
}
.body .list .list-border .list-item:nth-child(17) {
  border-left: 2rpx solid #dfdfdf;
  border-right: 2rpx solid #dfdfdf;
}
.body .list .list-border .list-item {
  width: 30%;
  margin: 0 10rpx;
  margin-bottom: 40rpx;
  font-weight: 500;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-break: break-all;
}

</style>